package com.baichen;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicInteger;

public class MyInterfaceFactory {

    private static final AtomicInteger counter = new AtomicInteger(0);

    public static MyInterface createProxyInstance(MyHandler myHandler) throws Exception {
        String className = getClassName();
        File javaClassFile = createJavaFile(myHandler, className);
        File compileJavaFile = Compiler.compileJavaFile(javaClassFile);
        if (compileJavaFile != null) {
            return newProxyInstance("com.baichen." + className, myHandler);
        } else {
            System.err.println("编译失败");
        }
        return null;
    }

    private static String getClassName() {
        return "MyInterface$proxy" + counter.incrementAndGet();
    }

    private static File createJavaFile(MyHandler myHandler, String className) throws IOException {
        String func1 = myHandler.functionBody("func1");
        String func2 = myHandler.functionBody("func2");
        String func3 = myHandler.functionBody("func3");

        String fieldName = getFieldName(myHandler);
        
        String context = "package com.baichen;\n" +
                "\n" +
                "public class " + className + " implements MyInterface {\n" +
                "    MyInterface " + fieldName + ";\n" +
                "    @Override\n" +
                "    public void func1() {\n" +
                "        " + func1 + "\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    public void func2() {\n" +
                "        " + func2 + "\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    public void func3() {\n" +
                "        " + func3 + "\n" +
                "    }\n" +
                "}\n";
        File packageDir = new File("src/main/java/com/baichen");
        if (!packageDir.exists()) {
            packageDir.mkdirs();
        }
        File javaClassFile = new File(packageDir, className + ".java");
        Files.writeString(javaClassFile.toPath(), context);
        return javaClassFile;
    }
    
    /**
     * 根据处理器类型返回对应的字段名
     */
    private static String getFieldName(MyHandler myHandler) {
        if (myHandler instanceof BeforeHandler || 
            myHandler instanceof AfterHandler || 
            myHandler instanceof AroundHandler) {
            return "target";
        } else {
            return "myInterface";  // 保持向后兼容性
        }
    }

    private static MyInterface newProxyInstance(String fqcn, MyHandler myHandler) throws Exception {
        Class<?> clazz = Class.forName(fqcn);
        Constructor<?> constructor = clazz.getConstructor();
        MyInterface myInterface = (MyInterface) constructor.newInstance();
        myHandler.setMyInterface(myInterface);
        return myInterface;
    }
}
